package top.ucat.x.file.util;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.auth.sts.AssumeRoleRequest;
import com.aliyuncs.auth.sts.AssumeRoleResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.http.ProtocolType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import top.ucat.x.file.beans.AliStsVo;
import top.ucat.x.system.entity.SysAliOss;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Auther: Jun
 * @Date: 2019/6/25 12:05
 * @Description:
 */
public class AliossUtil {

    static String POLICY = "{\"Statement\":[{\"Action\":[\"oss:*\"],\"Effect\":\"Allow\",\"Resource\":[\"acs:oss:*:*:*\"]}],\"Version\":\"1\"}";

    static String STS_API_VERSION = "2015-04-01";

    static ProtocolType PROTOOL_TYPE = ProtocolType.HTTPS;


    public static AliStsVo getStsToken(SysAliOss alioss, String module, String userId) {
        String appKey = alioss.getAppKey();
        String appSecret = alioss.getAppSecret();
        String roleArn = alioss.getRoleArn();
        String roleName = alioss.getRoleName();
        long durationSeconds = alioss.getDurationSeconds();
        String region = alioss.getRegion();

        IClientProfile profile = DefaultProfile.getProfile(region, appKey, appSecret);
        DefaultAcsClient client = new DefaultAcsClient(profile);


        // 创建一个 AssumeRoleRequest 并设置请求参数
        final AssumeRoleRequest request = new AssumeRoleRequest();
        request.setVersion(STS_API_VERSION);
        request.setMethod(MethodType.POST);
        request.setProtocol(PROTOOL_TYPE);
        request.setRoleArn(roleArn);
        request.setRoleSessionName(roleName);
        request.setPolicy(POLICY);
        request.setDurationSeconds(durationSeconds);

        // 发起请求，并得到response
        try {
            AssumeRoleResponse response = client.getAcsResponse(request);
            AssumeRoleResponse.Credentials credentials = response.getCredentials();
            String fileStoreDir = AliossUtil.getFileStoreDir(module, userId);
            AliStsVo aliStsVo = AliStsVo.builder()
                    .accessKeyId(credentials.getAccessKeyId())
                    .accessKeySecret(credentials.getAccessKeySecret())
                    .securityToken(credentials.getSecurityToken())
                    .expiration(credentials.getExpiration())
                    .accessDomain(alioss.getAccessDomain())
                    .region(region)
                    .dir(fileStoreDir)
                    .bucketName(alioss.getBucketName())
                    .build();
            return aliStsVo;
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取文件保存的路径
     *
     * @param module
     * @param userId
     * @return
     */
    public static String getFileStoreDir(String module, String userId) {
        SimpleDateFormat sdf = new SimpleDateFormat("/yyyy/MM/dd/");
        String dir = module + "/" + userId + sdf.format(new Date());
        return dir;
    }

}
